<template>
  <div class="myDiv">
    <tou title="设备详情"></tou>
    <div>
      <p class="title">脉探设备信息</p>
      <van-field
        required
        v-model="deviceobj.deviceNo"
        label="设备编码："
        :readonly="isset"
        v-if="isadd"
      />
      <van-field required v-model="deviceobj.address" label="设备名称：" :readonly="isset" />
      <van-field v-model="deviceobj.company" placeholder="请输入放置地区" label="放置地区:" :readonly="isset" />
      <div v-if="this.$store.state.userinfo.userkind==1">
        <p class="title">体检类型价格(元)</p>
        <van-field
          v-for="(v,i) in datatype"
          :key="i"
          v-model="deviceobj.price[i]"
          label-width="150px"
          type="number"
          :label="v.value+':'"
          :readonly="isset"
          @input="filter(i)"
        />
      </div>
    </div>
    <van-cell size="large" class="btns" v-if="!isadd">
      <van-button size="small" type="primary" v-if="isset" @click="isset=false">编辑</van-button>
      <van-button size="small" type="primary" v-if="!isset" @click="isset=true">取消</van-button>
      <van-button
        size="small"
        type="primary"
        v-if="!isset"
        class="btn"
        @click="saveOrganization"
      >确认修改</van-button>
      <van-button size="small" type="danger" v-if="isset" @click="del">删除</van-button>
    </van-cell>
    <van-cell size="large" class="btns" v-if="isadd">
      <van-button size="small" type="primary" @click="setmsg(1)">取消</van-button>
      <van-button size="small" type="primary" @click="setmsg(2)">确认添加</van-button>
    </van-cell>
  </div>
</template>
    
    <script>
import tou from "@/components/header";
export default {
  components: { tou },
  props: {},
  data() {
    return {
      msg: "",
      isset: true, //是否禁用输入框
      isadd: false,
      deviceobj: {
        idcode: "",
        deviceNo: "",
        address: "",
        company: "",
        orgcode: "",
        name: [],
        price: []
      },
      datatype: [] //体检类型价格数据
    };
  },
  watch: {},
  computed: {},
  methods: {
    setmsg(a) {
      if (a == 1) {
        this.$router.go(-1);
      } else {
        //   添加按钮
        this.$http(this.api.addOrganization, {
          deviceNo: this.deviceobj.deviceNo,
          address: this.deviceobj.address,
          company: this.deviceobj.company,
          version: this.deviceobj.name.join(","),
          orgname: this.deviceobj.price.join(",")
        }).then(res => {
          if (res.status === 200) {
            if (res.data.result == "true") {
              this.Tips(res.data.message, "success");
              this.$router.go(-1);
            } else {
              this.Tips(res.data.message, "warning");
            }
          }
        });
      }
      this.isset = false;
    },
    del() {
      this.$dialog
        .confirm({
          title: "提示",
          message: "确定要删除吗"
        })
        .then(() => {
          this.$http(this.api.deleteOrganization, {
            idcode: this.deviceobj.idcode,
            deviceNo: this.deviceobj.deviceNo,
            orgcode: this.deviceobj.orgcode
          }).then(res => {
            if (res.status === 200) {
              if (res.data.result == "true") {
                this.Tips(res.data.message, "success");
                this.$router.go(-1);
              } else {
                this.Tips(res.data.message, "warning");
              }
            }
          });
        })
        .catch(() => {});
    },
    // 过滤输入的值(如果小于0等于0)
    filter(i) {
      if (this.deviceobj.price[i] < 0) {
        this.deviceobj.price[i] = 0;
      }
    },
    // 确认修改的按钮
    saveOrganization() {
      if (this.$store.state.userinfo.userkind == 1) {
        this.$http(this.api.saveOrganization, {
          deviceNo: this.deviceobj.deviceNo,
          orgcode: this.deviceobj.orgcode,
          idcode: this.deviceobj.idcode,
          address: this.deviceobj.address,
          company: this.deviceobj.company,
          version: this.deviceobj.name.join(","),
          orgname: this.deviceobj.price.join(",")
        }).then(res => {
          if (res.status === 200) {
            if (res.data.result == "true") {
              this.Tips(res.data.message, "success");
              this.isset = true;
            } else {
              this.Tips(res.data.message, "warning");
            }
          }
        });
      } else {
        this.$http(this.api.saveOrganization, {
          idcode: this.deviceobj.idcode,
          address: this.deviceobj.address,
          company: this.deviceobj.company,
          orgcode: this.deviceobj.orgcode
        }).then(res => {
          if (res.status === 200) {
            if (res.data.result == "true") {
              this.Tips(res.data.message, "success");
              this.isset = true;
            } else {
              this.Tips(res.data.message, "warning");
            }
          }
        });
      }
    },
    // 获取默认体检类型信息
    selectDatatype() {
      this.$http(this.api.selectDataTypes).then(res => {
        if (res.status == 200) {
          if (res.data.result == "true") {
            res.data.pageData.data.map((v, i) => {
              this.deviceobj.price[i] = v.price;
              this.deviceobj.name[i] = v.name;
            });
            this.datatype = res.data.pageData.data;
          }
        }
      });
    }
  },
  created() {
    // 判断用户进来点击的是添加还是修改
    // 如果是添加
    if (this.$route.query.isadd) {
      this.isadd = true;
      this.isset = false;
      // 判断用户的权限
      if (this.$store.state.userinfo.userkind == 1) {
        // 购买用户获取默认体检类型信息
        this.selectDatatype();
      }
    } else {
      // 如果是修改
      //   获取修改的item数据
      let data = this.$route.query.data;
      this.deviceobj.address = data.address;
      this.deviceobj.company = data.company;
      this.deviceobj.idcode = data.idcode;
      this.deviceobj.orgcode = data.orgcode;
      this.deviceobj.deviceNo = data.deviceNo;
      this.deviceobj.company = data.company;
      this.deviceobj.address = data.address;
      //   判断用户权限
      if (this.$route.query.result) {
        this.$route.query.result.map((v, i) => {
          this.deviceobj.price[i] = v.price;
          this.deviceobj.name[i] = v.name;
        });
        this.datatype = this.$route.query.result;
      }
    }
  },
  mounted() {}
};
</script>
    <style scoped>
.btns {
  width: max-content;
  margin-left: 50%;
  transform: translateX(-50%);
}
.title {
  color: #4ce8d3;
  font-size: 18px;
  text-align: center;
  line-height: 18px;
  margin: 10px 0;
}
</style>